<?php
/**
 * Created by PhpStorm.
 * User: iamya
 * Date: 2017/6/23
 * Time: 13:52
 */

namespace app\plus\controller;


use SimpleXMLElement;
use think\Controller;
use think\Db;
use think\Request;

class SeoController extends Controller
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    public function sitemap()
    {
        $cmsCate = Db::table('cms_category')->cache(86400)->paginate(1000);
        $cmsCateLast = $cmsCate->lastPage();
        $goodsCate = Db::table('shop_goods_category')->cache(86400)->paginate(1000);
        $goodsCateLast = $goodsCate->lastPage();
        $cmsList = Db::table('cms_content')->cache(86400)->paginate(1000);
        $cmsLast = $cmsList->lastPage();
        $goodsList = Db::table('shop_goods_sku')->cache(86400)->paginate(1000);
        $goodsLast = $goodsList->lastPage();
        $selfList = Db::table('seo')->cache(86400)->paginate(1000);
        $selfLast = $selfList->lastPage()?:1;
        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><sitemapindex />');
        for ($i = 1; $i <= $selfLast; $i++) {
            $sitemap = $xml->addChild('sitemap');
            $sitemap->addChild('loc', url('/plus/Seo/self/page/' . $i, '', '', true));
            $sitemap->addChild('lastmod', date('Y-m-d'));
        }
        for ($i = 1; $i <= $cmsCateLast; $i++) {
            $sitemap = $xml->addChild('sitemap');
            $sitemap->addChild('loc', url('/plus/Seo/cmsCate/page/' . $i, '', '', true));
            $sitemap->addChild('lastmod', date('Y-m-d'));
        }
        for ($i = 1; $i <= $goodsCateLast; $i++) {
            $sitemap = $xml->addChild('sitemap');
            $sitemap->addChild('loc', url('/plus/Seo/goodsCate/page/' . $i, '', '', true));
            $sitemap->addChild('lastmod', date('Y-m-d'));
        }
        for ($i = 1; $i <= $cmsLast; $i++) {
            $sitemap = $xml->addChild('sitemap');
            $sitemap->addChild('loc', url('/plus/Seo/cmsCont/page/' . $i, '', '', true));
            $sitemap->addChild('lastmod', date('Y-m-d'));
        }
        for ($i = 1; $i <= $goodsLast; $i++) {
            $sitemap = $xml->addChild('sitemap');
            $sitemap->addChild('loc', url('/plus/Seo/goodsCont/page/' . $i, '', '', true));
            $sitemap->addChild('lastmod', date('Y-m-d'));
        }
        ob_end_clean();
        header("Content-type: text/xml");
        echo $xml->asXml();
    }

    public function self()
    {
        $page = $this->request->get('page', 1);
        $data = Db::table('seo')->cache(86400)->page($page, 1000)->select()->toArray();
        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><urlset />');
        if ($page==1){
            $url = $xml->addChild('url');
            $url->addChild('loc', url('/home/Index/index','','',true));
            $url->addChild('lastmod', date('Y-m-d'));
            $url->addChild('changefreq', 'always');
            $url->addChild('priority', 1);
        }
        foreach ($data as $value) {
            $url = $xml->addChild('url');
            $url->addChild('loc', str_replace('&', '&amp;', $value['url']));
            $url->addChild('lastmod', $value['lastmod']);
            $url->addChild('changefreq', $value['changefreq']);
            $url->addChild('priority', $value['priority']);
        }
        ob_end_clean();
        header("Content-type: text/xml");
        echo $xml->asXml();
    }

    public function goodsCate()
    {
        $page = $this->request->get('page', 1);
        $data = Db::table('shop_goods_category')->cache(86400)->page($page, 1000)->select()->toArray();
        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><urlset />');
        $request = Request::instance();
        foreach ($data as $value) {
            $val = format_goods_cat($value);
            $url = $xml->addChild('url');
            $url->addChild('loc', $request->domain() . str_replace('&', '&amp;', $val['url']));
            $url->addChild('lastmod', date('Y-m-d'));
            $url->addChild('changefreq', 'daily');
            $url->addChild('priority', 0.8);
        }
        ob_end_clean();
        header("Content-type: text/xml");
        echo $xml->asXml();
    }

    public function cmsCate()
    {
        $page = $this->request->get('page', 1);
        $data = Db::table('cms_category')->cache(86400)->page($page, 1000)->select()->toArray();
        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><urlset />');
        $request = Request::instance();
        foreach ($data as $value) {
            $val = format_cms_cat($value);
            $url = $xml->addChild('url');
            $url->addChild('loc', $request->domain() . str_replace('&', '&amp;', $val['url']));
            $url->addChild('lastmod', date('Y-m-d'));
            $url->addChild('changefreq', 'daily');
            $url->addChild('priority', 0.8);
        }
        ob_end_clean();
        header("Content-type: text/xml");
        echo $xml->asXml();
    }

    public function cmsCont()
    {
        $page = $this->request->get('page', 1);
        $data = Db::table('cms_content')->cache(86400)->page($page, 1000)->select()->toArray();
        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><urlset />');
        $request = Request::instance();
        foreach ($data as $value) {
            $val = format_cms_content($value);
            $url = $xml->addChild('url');
            $url->addChild('loc', $request->domain() . str_replace('&', '&amp;', $val['url']));
            $url->addChild('lastmod', date('Y-m-d'));
            $url->addChild('changefreq', 'daily');
            $url->addChild('priority', 0.5);
        }
        ob_end_clean();
        header("Content-type: text/xml");
        echo $xml->asXml();
    }

    public function goodsCont()
    {
        $page = $this->request->get('page', 1);
        $data = Db::table('shop_goods_sku')->cache(86400)->page($page, 1000)->select()->toArray();
        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><urlset />');
        $request = Request::instance();
        foreach ($data as $value) {
            $val = format_goods_content($value);
            $url = $xml->addChild('url');
            $url->addChild('loc', $request->domain() . str_replace('&', '&amp;', $val['url']));
            $url->addChild('lastmod', date('Y-m-d'));
            $url->addChild('changefreq', 'daily');
            $url->addChild('priority', 0.5);
        }
        ob_end_clean();
        header("Content-type: text/xml");
        echo $xml->asXml();
    }
}